using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Drawing;
using BodySoundSimulator.Properties;

namespace BodySoundSimulator.AbsoluteMapData
{
    class AbsoluteBodyFeaturesDataManipulator
    {
        public static AbsoluteBodyFeatures LoadAbsoluteBodyFeatureData()
        {
            AbsoluteBodyFeatures data = null;
            StreamReader reader = null;
            try
            {
                reader = new StreamReader(Path.Combine(Application.StartupPath, "RibCageData.xml"));
                XmlSerializer deserializer = new XmlSerializer(typeof(AbsoluteBodyFeatures));
                data = deserializer.Deserialize(reader) as AbsoluteBodyFeatures;
            }
            catch
            {
                data = new AbsoluteBodyFeatures();
                data.FillDummyInfo();
                SaveAbsoluteBodyFeatures(data);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return data;
        }

        public static void SaveAbsoluteBodyFeatures(AbsoluteBodyFeatures data)
        {
            if (data == null)
            {
                data = new AbsoluteBodyFeatures();
            }
            StreamWriter streamWriter = null;
            try
            {
                streamWriter = new StreamWriter(Path.Combine(Application.StartupPath, "RibCageData.xml"));
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(AbsoluteBodyFeatures));
                xmlSerializer.Serialize(streamWriter, data);
            }
            catch (System.IO.IOException)
            {
            }
            finally
            {
                if (streamWriter != null)
                {
                    streamWriter.Close();
                }
            }
        }

        public static void DrawFrontView(Graphics g)
        {
            DrawFullRibCage(g);
        }

        public static void DrawBackView(Graphics g)
        {
            DrawFullRibCage(g);
        }

        #region Private methods
        private static void DrawFullRibCage(Graphics g)
        {
			Bitmap ribCageImage = Resources.RigCage;
			g.DrawImage(ribCageImage, new Point(-AbsoluteBodyFeatures.OriginTranslateX, -AbsoluteBodyFeatures.OriginTranslateY));
        }        
        #endregion
    }
}
